﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon.Validators;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCommon.Prompt;

namespace Baosight.ZNH.ZMCD.UI
{
    public partial class ZMCD04 : EFViewCmes
    {
        public ZMCD04()
        {
            InitializeComponent();            
        }

        public OperationMode ModeType
        {
            get { return Mode; }
            set { Mode = value; }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            //base.OnCommitButtonClick(sender, e);
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
           
            TempButton = sender as Button;
            TempButton.IsEnabled = false;

            //ValidatorManager自定义的校验方法
            bool result = true;
            ValidatorManager group = this.GetChildObject<ValidatorManager>(null);
            if (group != null)
            {
                result = group.IsValidSucces;
            }
            bool _validateField = ThisValidateField() & this.ValidateField() & result;//确定前的效验

            if (!_validateField)
            {
                this.ValidateMessagePopup("输入不合法, 请检查输入的数据！");//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            if (this.upload.GridRowCount <= 0)
            {
                this.ValidateMessagePopup("上传文件不能为空！");//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            if (this.upload.IsBusy)
            {
                this.ValidateMessagePopup("文件正在上传中，请稍候再提交数据！");//弹出提示信息
                this.TempButtonOpen(true);//确定按钮生效
                return;
            }

            EIInfo eiInfo = this.BuildSubmitEIInfo(eServiceName, Mode.ToString().ToLower(), EIConstants.ResultBlock);
            Dictionary<string, string> dic = upload.InsertFileArgs;
            EIBlock inquBlock = eiInfo.AddBlock("insertfiles");
            EIColumn dir = new EIColumn();
            dir.Name = "fdKeyId";
            dir.TypeLabel = "C";
            inquBlock.AddColumn(dir);

            dir = new EIColumn();
            dir.Name = "fdMemoryName";
            dir.TypeLabel = "C";
            inquBlock.AddColumn(dir);

            dir = new EIColumn();
            dir.Name = "fdForeignId";
            dir.TypeLabel = "C";
            inquBlock.AddColumn(dir);

            foreach (string obj in dic.Keys)
            {
                EIRow row = new EIRow();
                row.Add("fdKeyId", dic[obj]);
                row.Add("fdMemoryName", obj);
                row.Add("fdForeignId", fdKeyId.Value);
                inquBlock.AddRow(row);
            }

            base.Submit(eiInfo, DoCommitCompleted);
        }

        /// <summary>
        /// 弹出的新增、修改页面点击"确定"后的回调
        /// 具体实现内中可以重构
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private new void DoCommitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            if (inInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                string str = inInfo.GetMsg() == null ? "提交数据出错！" : inInfo.GetMsg();
                this.ValidateMessagePopup(str);//操作失败，消息提醒
            }
            else
            {
                //操作成功关闭新增页面
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            EFWindow _window = this.Parent as EFWindow;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 验证失败，消息提示
        /// </summary>
        public void ValidateMessagePopup(string msg)
        {
            Prompts pop = Prompts.getInstance();
            pop.SetMsg(msg);
            pop.Show(6);

            return;
        }
    }
}
